package cc.linker.steplink.crm.merchanth5.controller;
import cc.linker.steplink.crm.commons.oss.OssService;
import cc.linker.steplink.crm.commons.utils.WatermarkUtil;
import cc.linker.steplink.crm.response.GlobalException;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/6/9 15:37
 * @Description: TODO
 */
@RestController
@RequestMapping("/h5/upload")
public class FileUploadController {
    @Autowired
    private OssService ossService;

    private static final String uploadFirstSubDir = "store";

    @PostMapping("/uploadImg")
    public Object uploadImg(MultipartFile file, HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String path = null;// 文件路径
        if (file != null) {
            String type = null;// 文件类型
            String fileName = file.getOriginalFilename();// 文件原名称
            // 判断文件类型
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {// 判断文件类型是否为空
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase()) || "JPEG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    File directory = new File("");// 参数为空
                    String realPath = directory.getCanonicalPath();
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + "." + type;
                    // 设置存放图片文件的路径
                    path = realPath + "/upload/" + trueFileName + "/";
                    File dirFile = new File(path);
                    if (!dirFile.exists()) {
                        dirFile.mkdirs();
                    }
                    // 转存文件到指定的路径
                    String sourceImgPath = path + fileName;
                    file.transferTo(new File(sourceImgPath));

                    //region 加水印
                    String targerTextPath = path + "mark_"+fileName;
                    String markTxt = "供货商城专用";
                    WatermarkUtil.markImageByText(markTxt,sourceImgPath,targerTextPath,type);
                    //endregion 水印加完

                    File imgFile = new File(targerTextPath);
                    if(imgFile.isFile()){
                        FileInputStream fileInputStream = new FileInputStream(imgFile);
                        MultipartFile multipartFile = new MockMultipartFile(imgFile.getName(), imgFile.getName(),
                                ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
                        String url = ossService.uploadLocalFileToOss(uploadFirstSubDir, "StepLink", multipartFile);
                        map.put("url", url);
                        imgFile.delete();
                        new File(sourceImgPath).delete();
                    }else{
                        throw GlobalException.build("上传图片失败");
                    }
                }
            } else {
                throw GlobalException.build("只能上传jpg、jpeg、png、gif类型图片");
            }
        } else {
            throw GlobalException.build("请选择要上传的图片");
        }
        return map;
    }
}
